Imagine we are working in a class called Class1%, with init-fields x, y, and z (all containing integers).
Consider the following method definition:
;; Number -> Class1% ;; GIVEN: a number u ;; RETURNS: an object just like this one, except that the new x field contains ;; x+y, and the new y field contains z+u. (define/public (method1 u) (new Class1% [x (+ x y)] [y (+ z u)] [z z]))
Your task is to transform this into an imperative method using the Void transform. The new contract and purpose statement are:
;; Number -> Void ;; GIVEN: a number u ;; EFFECT: alters the fields of this object so that the new x field ;; contains the value of x+y, and the new y field contains the value ;; of z+u. ;; EXAMPLE: if obj1 has fields x=10, y=20, and z=100, then calling ;; (send obj1 method1 5) ;; should leave obj1 with fields x=30, y=105, z=100.
Which of the following are correct versions of the new method?
;;; Version 1: (define/public (method1 u) (new Class1% [x (+ x y)] [y (+ z u)])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Version 2: (define/public (method1 u) (set! x (+ x y)) (set! y (+ z u))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Version 3: (define/public (method1 u) (set! x (+ x y)) (set! y (+ z u)) (set! z z)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Version 4: (define/public (method1 u) (set! x (+ x y)) (set! z z) (set! y (+ z u))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Version 5: (define/public (method1 u) (set! y (+ z u)) (set! x (+ x y)))
Last modified: Tue Nov 10 22:32:45 Eastern Standard Time 2015